<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 08.12.11
 * Time: 2:03
 * To change this template use File | Settings | File Templates.
 */

class Question_Question extends Core_BaseModel
{
    public function getAllQuestionToLesson($idLesson)
    {
        //echo "getAllQuestionToLesson($idLesson) <br/>";
        $pdo = $this->dataModule->getConnection();
        $sql = "SELECT `idquestion`,`textQuestion`,`isNeedFullTextAsnwer`,`lesson_idlesson` FROM `dzundza_basequestion` as `que`
where `que`.`lesson_idlesson`  = :idLesson";

        $stn = $pdo->prepare($sql);
        $stn->bindParam(':idLesson', $idLesson, PDO::PARAM_INT);
        $stn->execute();
        $result = $stn->fetchAll(PDO::FETCH_ASSOC);
        $out = array();
        foreach ($result as $key => $value) {
            $out[$value['idquestion']] = array(
                'text' => $value['textQuestion'],
                'needFullAns' => $value['isNeedFullTextAsnwer'],
                'idlesson' => $value['lesson_idlesson']
            );
        }
        return $out;

    }

    public function getCorrectValueFromQuestion($idQuestion)
    {
        $pdo = $this->dataModule->getConnection();

        $sql = "SELECT `answers_idanswers` FROM `dzundza_multiplechoicevariants`
WHERE `isCorrect` = true AND `baseQuestion_idquestion` = :idQuestion";
        $stn = $pdo->prepare($sql);
        $stn->bindValue(':idQuestion', $idQuestion, PDO::PARAM_INT);
        $stn->execute();
        $result = $stn->fetch(PDO::FETCH_COLUMN);
        //var_dump($stn->errorInfo());
        return $result;


    }

    public function addBaseQuestion($lessonId, $textQuestion, $userProfile)
    {
        try {
            //echo "<pre> addBaseQuestion($lessonId, $textQuestion, $userProfile)<br/></pre>";
            $pdo = $this->dataModule->getConnection();
            $pdo->beginTransaction();
            $sql = "INSERT INTO `dzundza_basequestion`(`lesson_idlesson`,`textQuestion`,`userProfile_iduserProfile`)
                VALUES(:lessonid,:text,:userProfile)";

            $stn = $pdo->prepare($sql);
            $stn->bindParam(':lessonid', $lessonId, PDO::PARAM_INT);
            $stn->bindParam(':text', $textQuestion, PDO::PARAM_STR);
            $stn->bindParam(':userProfile', $userProfile, PDO::PARAM_INT);
            $stn->execute();

            $id = $pdo->lastInsertId('idquestion');
            $pdo->commit();
            //var_dump($id);
            return $id;
        } catch (PDOException $e) {
            $pdo->rollBack();
            echo "<pre>";
            echo "Error $e<br/>";
            echo "</pre>";


        }
    }


}
